<template>
  <div class="add">
    <el-dialog
      :title="{ add: '新增', edit: '编辑' }[meta]"
      :visible="value"
      width="600px"
      @close="closeEvent"
      center
    >
      <el-form :model="form" label-width="150px" ref="form" :rules="rules">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model="form.eid"> </el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name"> </el-input>
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model="form.short_name"> </el-input>
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model="form.intro"> </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"> </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { enterpriseAdd, enterpriseEdit } from '@/api/business.js'
export default {
  props: ['value'],
  data () {
    return {
      meta: 'add',
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        eid: [{ required: true, message: '必填', tirgger: 'change' }],
        name: [{ required: true, message: '必填', tirgger: 'change' }],
        short_name: [{ required: true, message: '必填', tirgger: 'change' }],
        intro: [{ required: true, message: '必填', tirgger: 'change' }]
      }
    }
  },
  methods: {
    closeEvent () {
      this.$emit('input', false)
      // 清除表单验证
      this.$refs.form.resetFields()
      // 重置表单
      this.form = {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    },
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          // 如果是新增
          if (this.meta === 'add') {
            console.log('验证成功')
            const res = await enterpriseAdd(this.form)
            console.log('新增', res)
            this.$message.success('新增成功')
          } else if (this.meta === 'edit') {
            // 如果是编辑
            const res = await enterpriseEdit(this.form)
            console.log('编辑', res)
            this.$message.success('更新成功')
          }
          this.$emit('getData')
          // 重置表单
          this.closeEvent()
        }
      })
    }
  }
}
</script>

<style lang="less">
.el-dialog__header {
  padding: 20px 20px 10px;
  background-color: #08b2fa;
}
</style>
